package com.yunsuo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunsuo.bean.*;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.management.LockInfo;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Hello world!
 *
 */

public class App 
{

    public static void main(String[] args) {

        int l_id_A=0;
        String l_uuid_A="";
        // 1.加载数据访问驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //2.连接到数据"库"上去
        Connection conn= null;
        try {
//            conn = DriverManager.getConnection("jdbc:mysql://192.168.43.247:3306/raspberry?characterEncoding=UTF-8", "root", "123456");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/raspberry?characterEncoding=UTF-8", "root", "123456");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //3.构建SQL命令
        Statement state= null;
        try {
            state = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql="select * from lock_raspberry";
        PreparedStatement pstmt= null;
        try {
            pstmt = conn.prepareStatement(sql);
            ResultSet rs=pstmt.executeQuery();
            int l_id=0;
            String l_uuid=null;
            String l_introduction="";
            while(rs.next()){
                l_id=rs.getInt("l_id");
                l_uuid=rs.getString("l_uuid");
                ////System.out.println(l_id+":"+l_uuid);
            }
            if(l_id==0 && l_uuid==null){
                final HttpConnectionFinal httpConnection = new HttpConnectionFinal();
                LockRaspberry lock=new LockRaspberry(0,null,null,
                        null, null, null, null, null,
                        null, null);
                String relativelyPath=System.getProperty("user.dir");
                String pathname = relativelyPath+"/introduction.txt"; // 绝对路径或相对路径都可以，写入文件时演示相对路径,读取以上路径的input.txt文件
                ////System.out.println("relativelyPath="+pathname);
                String introduction="";
                //防止文件建立或读取失败，用catch捕捉错误并打印，也可以throw;
                //不关闭文件会导致资源的泄露，读写文件都同理
                //Java7的try-with-resources可以优雅关闭文件，异常时自动关闭文件；详细解读https://stackoverflow.com/a/12665271
                try (FileReader reader = new FileReader(pathname);
                     BufferedReader br = new BufferedReader(reader) // 建立一个对象，它把文件内容转成计算机能读懂的语言
                ) {
                    String line;
                    //网友推荐更加简洁的写法
                    while ((line = br.readLine()) != null) {
                        // 一次读入一行数据
                        introduction=line;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Map<String,Object> map=new HashMap<>();
                map.put("method","createLock");
                map.put("introduction",introduction);
                JSONObject json = new JSONObject(map);
                String jsonStr = JSONObject.toJSONString(json);
                ////System.out.println("jsonStr==================="+jsonStr);
                final String info = "info="+jsonStr;
                String result = httpConnection.connection("/raspberry/createLock",info);
                ////System.out.println("createResult="+result);
                JSONObject jsonObject = JSON.parseObject(result);

                LockRaspberry lockRaspberry=new LockRaspberry(jsonObject.getInteger("lId")
                ,jsonObject.getString("lUuid"),jsonObject.getInteger("lStatus"),
                        jsonObject.getString("lSupplier"),jsonObject.getString("lSoftVer"),
                        jsonObject.getString("lHardVer"),jsonObject.getString("lProductionDate"),
                        jsonObject.getString("lIntroduction"),0,0);

                // 1.加载数据访问驱动
                //Class.forName("com.mysql.cj.jdbc.Driver");
                //2.连接到数据"库"上去
                //Connection conn= DriverManager.getConnection("jdbc:mysql://192.168.43.247:3306/raspberry?characterEncoding=UTF-8", "root", "123456");
                //3.构建SQL命令
                //Statement state=conn.createStatement();
                String s="insert into lock_raspberry(l_id,l_uuid,l_status,l_supplier,l_soft_ver,l_hard_ver," +
                        "l_production_date,l_introduction,l_delete,l_modify) " +
                        "VALUES("+jsonObject.getInteger("lId")+",'"+jsonObject.getString("lUuid")
                        +"',"+jsonObject.getInteger("lStatus")+",'"+jsonObject.getString("lSupplier")
                        +"','"+jsonObject.getString("lSoftVer")+"','"+jsonObject.getString("lHardVer")+"','"
                        +jsonObject.getString("lProductionDate")+"','"+jsonObject.getString("lIntroduction")
                        +"',"+0+","+0+")";
                ////System.out.println("sql222:"+s);
                l_id_A=jsonObject.getInteger("lId");
                l_uuid_A=jsonObject.getString("lUuid");
                state.executeUpdate(s);
            }else{
                l_id_A=l_id;
                l_uuid_A=l_uuid;
            }
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            conn.close();//关闭连接
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //创建一个开门搜索指纹的线程
        new TestThread().start();
        new DoorStatusThread().start();


        while(true){
            CallHttp.request(l_id_A,l_uuid_A);
            try{
                //Thread.sleep(2000);
                ////System.out.println("sleep 2 seconds");

            }catch(Exception e){
                System.exit(0);//退出程序
            }
        }

    }
}
